﻿using System;
using System.Windows.Forms;
using System.Xml;
using Core;
using ClassLibrary;
namespace TestProject
{
    public partial class NewGeneTable : Form
    {
        private Model model;
        public NewGeneTable(Model m)
        {
            InitializeComponent();
            model = m;
        }

        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void create_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Tables\\GeneTables\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A gene table with this name already exists. Please, choose another name.");
                return;
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" +model.model_name + "\\Tables\\GeneTables\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("gene_table");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("GeneCategoriesAmount", amount_box.Value.ToString());
            writer.WriteStartElement("genes");
            for (int i = 0; i < Convert.ToInt32(amount_box.Text); i++)
            {
                writer.WriteStartElement("gene");
                writer.WriteAttributeString("Id", i.ToString());
                writer.WriteAttributeString("Name", "default");
                writer.WriteAttributeString("AmountAllels", "0");
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("table");
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Tables\\GeneTables\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("tables")[0].Attributes[0]);
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.GetElementsByTagName("gene_tables")[0].AppendChild(el);
            doc.Save(model.model_path);
            Functions.add_node_func(new GeneTable("file_input\\" + model.model_name + "\\Tables\\GeneTables\\" + name_box.Text + ".xml"), name_box.Text, "gent");
            this.Close();
        }
    }
}
